package algorithm.alternateprinting;

import java.util.concurrent.locks.LockSupport;

/**
 * @auther zhouliangliang
 * @date 2022/10/8 14:11
 * @description 两个线程交替打印
 * 使用 LockSupport
 */
public class AlternatePrintingOne {
    Thread thread1 = new Thread(new Runnable() {
        @Override
        public void run() {
            for(int i=0; i<=10; i++) {
                System.out.println(i);
                LockSupport.unpark(thread2);
                LockSupport.park(thread1);
            }
        }
    });

    Thread thread2 = new Thread(new Runnable() {
        @Override
        public void run() {
            for(int i = 'A'; i<='J'; i++) {
                LockSupport.park(thread2);
                System.out.println((char) i);
                LockSupport.unpark(thread1);
            }
        }
    });

    private void run() {
        thread1.start();
        thread2.start();
    }


    public static void main(String[] args) {
        new AlternatePrintingOne().run();
    }
}